package com.yaowk.sfp.system.server.controller;

import com.yaowk.sfp.common.base.BaseException;
import com.yaowk.sfp.common.base.PageReq;
import com.yaowk.sfp.common.base.PageResp;
import com.yaowk.sfp.common.base.StatusDict;
import com.yaowk.sfp.system.api.UserApi;
import com.yaowk.sfp.system.api.model.Role;
import com.yaowk.sfp.system.api.model.User;
import com.yaowk.sfp.system.api.model.view.PasswordModel;
import com.yaowk.sfp.system.server.service.IRoleService;
import com.yaowk.sfp.system.server.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "用户管理")
public class UserController implements UserApi {

    @Autowired
    private IUserService userService;
    @Autowired(required = false)
    private PasswordEncoder passwordEncoder;
    @Autowired
    private IRoleService roleService;

    @Override
    @ApiOperation("查询用户列表")
    public PageResp<User> findAll(User user, PageReq pageReq) {
        if (ObjectUtils.isEmpty(user.getStatus())) {
            user.setStatus(StatusDict.TRUE);
        }
        return userService.findAll(user, pageReq);
    }

    @Override
    @ApiOperation("查询用户详情")
    public User findOne(@PathVariable Long id) {
        return userService.findById(id);
    }

    @Override
    @ApiOperation("保存用户")
    public User save(User user) {
        if (StringUtils.isNotBlank(user.getPassword()) && !ObjectUtils.isEmpty(passwordEncoder)) {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        return userService.save(user);
    }

    @Override
    @ApiOperation("删除用户")
    public void delete(@PathVariable Long id) {
        userService.deleteById(id);
    }

    @Override
    @ApiOperation("用户角色列表")
    public List<Role> roleList(@PathVariable Long userId) {
        return roleService.findRolesByUserId(userId);
    }

    @Override
    @ApiOperation("保存用户角色")
    public void editRoles(@PathVariable Long userId, @RequestParam(required = false) List<Long> roleId) {
        userService.roleEdit(userId, roleId);
    }

    @Override
    @ApiOperation("获取用户权限，授权系统使用，不做接口对接")
    public List<Long> findPermissions(@PathVariable Long userId) {
        return userService.findPermissionsByUserId(userId);
    }

    @Override
    @ApiOperation("修改密码")
    public void editPassword(PasswordModel passwordModel) {
        if (StringUtils.isAnyBlank(passwordModel.getPassword(), passwordModel.getRePassword())) {
            throw new BaseException("新密码不允许为空");
        }
        if (!passwordModel.getPassword().equals(passwordModel.getRePassword())) {
            throw new BaseException("新密码不一致");
        }
        User user = userService.findById(passwordModel.getId());
        if (!passwordEncoder.matches(passwordModel.getOldPassword(), user.getPassword())) {
            throw new BaseException("密码错误");
        }

        user.setPassword(passwordEncoder.encode(passwordModel.getPassword()));
        userService.save(user);
    }
}
